<?php
class MY_Controller extends CI_Controller{
	
	function __construct(){
		parent::__construct();
		$this->load->model("memberMod");
	}
		
	protected function finalizeView($config){
		$this->load->view('finalizeView/dashboard',$config);
	}
	
	protected function finalizeViewAdmin($config){
		$this->load->view('finalizeViewAdmin/dashboard',$config);
	}
	
	protected function finalizeViewSas($config){
		$this->load->view('finalizeViewSas/dashboard',$config);
	}
	
	protected function finalizeViewLogin($config){
//		$this->load->view('finalizeView/signin',$config);
	}

	/**
	 * router signature call function from ajax request
	 * @param unknown_type $func_name
	 * @author : musa
	 */
	function ajax($func_name, $a=false, $b=false, $c=false, $d=false){
		$this->{'_'.$func_name}($a, $b, $c, $d);
	}
	
	function validateSignin(){
		$this->load->model("memberMod");
		$admin = md5("own");
		$teacher = md5("teacher");
		$student = md5('student');
		if(get_cookie('logged')==md5('success') && $this->memberMod->validatemember(get_cookie('member'))==true && (get_cookie('level')==$admin || get_cookie('level')==$teacher || get_cookie('level')==$student )){
			redirect('dash');
		} else {
			if(get_cookie('logged')=="" || get_cookie('logged')==false){
				
			} else {
				redirect('home');
			}
		}
	}
	
	function validateSigninSas(){
		$this->load->model("memberMod");
		$admin = md5("own");
		$teacher = md5("teacher");
		$student = md5('student');
		
		if(get_cookie('logged')==md5('success') && $this->memberMod->validatemember(get_cookie('member'))==true && (get_cookie('level')==$admin || get_cookie('level')==$teacher || get_cookie('level')==$student )){
			redirect('sas');
		} else {
			if(get_cookie('logged')=="" || get_cookie('logged')==false){
				
			} else {
				redirect('home');
			}
		}
	}
	
	function validateLogin(){
    	$true = md5('success');
    	$coo = $this->input->cookie('logged',true);
//    	$coo = get_cookie('logged');
//    	echo $coo;
    	if(get_cookie('logged')==$true && $this->memberMod->validatemember(get_cookie('member'))){
    		return true;
    	} else {
    		redirect("login");
    		return false;
    	}
    }
	
	function validateAdmin(){
		$level = "";
		$this->load->model("memberMod");
		$id = get_cookie('member');
		$que = $this->db->query("select level from member where id_member='$id'");
		foreach ($que->result() as $r):
			$level = $r->level;
		endforeach;
		$tlevel = md5('own');
		if(get_cookie('logged')==md5('success') && $this->memberMod->validatemember($id)==true && $tlevel==$level){
			return true;
		} else {
			redirect('signin');
			return false;
		}
	}
	
	function validateSas(){
	$level = "";
		$this->load->model("memberMod");
		$id = get_cookie('member');
		$que = $this->db->query("select level from member where id_member='$id'");
		foreach ($que->result() as $r):
			$level = $r->level;
		endforeach;
		$slevel = md5('student');
		$tlevel = md5('teacher');
		$alevel = md5('own');
		if(get_cookie('logged')==md5('success') && $this->memberMod->validatemember($id)==true && $slevel==$level || $tlevel==$level || $alevel==$level){
			return true;
		} else {
			redirect('sign');
			return false;
		}
	}
	
	function validateSign(){
		$level = "";
		$this->load->model("memberMod");
		$id = get_cookie('member');
		$que = $this->db->query("select level from member where id_member='$id'");
		foreach ($que->result() as $r):
			$level = $r->level;
		endforeach;
		$tlevel = md5('own');
		if(get_cookie('logged')==md5('success') && $this->memberMod->validatemember($id)==true && $tlevel==$level){
			redirect('dash');
			return true;
		}	
	}
	
	
	function validateCookie(){
    	$true = md5('success');
    	$coo = $this->input->cookie('logged',true);
//    	$coo = get_cookie('logged');
//    	echo $coo;
    	if(get_cookie('logged')==$true && $this->memberMod->validatemember(get_cookie('member'))){
    		return true;
    	} else {
    		redirect("dash/signin");
    		return false;
    	}
    }
    
	function validateCookieSas(){
    	$true = md5('success');
    	$coo = $this->input->cookie('logged',true);
//    	$coo = get_cookie('logged');
//    	echo $coo;
    	if(get_cookie('logged')==$true && $this->memberMod->validatemember(get_cookie('member'))){
    		return true;
    	} else {
    		redirect("sign");
    		return false;
    	}
    }
    
	function validateAccount(){
    	$true = md5('success');
    	$coo = $this->input->cookie('logged',true);
//    	$coo = get_cookie('logged');
//    	echo $coo;
    	if(get_cookie('logged')==$true && $this->memberMod->validatemember(get_cookie('member'))){
    		redirect("dash");
    		return true;
    	} else {
//    		redirect("signin");
    		return false;
    	}
    }
	
	protected function images_large($picture_tmp,$size,$picture_name,$picture_type){
		switch($picture_type){
			case "image/jpeg": 
				$pic = imagecreatefromjpeg($picture_tmp);
				$width_comp  = imagesx( $pic );
				$height_comp  = imagesy( $pic );
				$new_width_comp = floor( $width_comp * ( $size / $height_comp  ) );
				$new_height_comp  = $size;
				$tmp_img_comp  = imagecreatetruecolor( $new_width_comp , $new_height_comp  );
				$up_comp  = imagecopyresized( $tmp_img_comp , $pic, 0, 0, 0, 0, $new_width_comp , $new_height_comp , $width_comp , $height_comp  );
				$pth_comp  = site_url("images/temp/$picture_name");
				ob_start();
				$inp_comp  = imagejpeg( $tmp_img_comp  );
				$create_images_large = ob_get_contents();	
				return $create_images_large;
			break;
			case "image/png": 
				$pic = imagecreatefrompng($picture_tmp);
				$width_comp  = imagesx( $pic );
				$height_comp  = imagesy( $pic );
				$new_width_comp = floor( $width_comp * ( $size / $height_comp  ) );
				$new_height_comp  = $size;
				$tmp_img_comp  = imagecreatetruecolor( $new_width_comp , $new_height_comp  );
				$up_comp  = imagecopyresized( $tmp_img_comp , $pic, 0, 0, 0, 0, $new_width_comp , $new_height_comp , $width_comp , $height_comp  );
				$pth_comp  = site_url("images/temp/$picture_name");
				ob_start();
				$inp_comp  = imagepng( $tmp_img_comp  );
				$create_images_large = ob_get_contents();	
				return $create_images_large;
			break;
			case "image/bmp": 
				$pic = imagecreatefromjpeg($picture_tmp);
				$width_comp  = imagesx( $pic );
				$height_comp  = imagesy( $pic );
				$new_width_comp = floor( $width_comp * ( $size / $height_comp  ) );
				$new_height_comp  = $size;
				$tmp_img_comp  = imagecreatetruecolor( $new_width_comp , $new_height_comp  );
				$up_comp  = imagecopyresized( $tmp_img_comp , $pic, 0, 0, 0, 0, $new_width_comp , $new_height_comp , $width_comp , $height_comp  );
				$pth_comp  = site_url("images/temp/$picture_name");
				ob_start();
				$inp_comp  = imagewbmp( $tmp_img_comp  );
				$create_images_large = ob_get_contents();	
				return $create_images_large;
			break;
			case "image/gif": 
				$pic = imagecreatefromgif($picture_tmp);
				$width_comp  = imagesx( $pic );
				$height_comp  = imagesy( $pic );
				$new_width_comp = floor( $width_comp * ( $size / $height_comp  ) );
				$new_height_comp  = $size;
				$tmp_img_comp  = imagecreatetruecolor( $new_width_comp , $new_height_comp  );
				$up_comp  = imagecopyresized( $tmp_img_comp , $pic, 0, 0, 0, 0, $new_width_comp , $new_height_comp , $width_comp , $height_comp  );
				$pth_comp  = site_url("images/temp/$picture_name");
				ob_start();
				$inp_comp  = imagegif( $tmp_img_comp  );
				$create_images_large = ob_get_contents();	
				return $create_images_large;
			break;
		}
	}
	
	protected function images_medium($picture_tmp,$size,$picture_name,$picture_type){
		switch($picture_type){
			case "image/jpeg":
				$pic = imagecreatefromjpeg($picture_tmp);
				$width_comp  = imagesx( $pic );
				$height_comp  = imagesy( $pic );
				$new_width_comp = floor( $width_comp * ( $size / $height_comp  ) );
				$new_height_comp  = $size;
				$tmp_img_comp  = imagecreatetruecolor( $new_width_comp , $new_height_comp  );
				$up_comp  = imagecopyresized( $tmp_img_comp , $pic, 0, 0, 0, 0, $new_width_comp , $new_height_comp , $width_comp , $height_comp  );
				$pth_comp  = site_url("images/temp/$picture_name");
				ob_start();
				$inp_comp  = imagejpeg( $tmp_img_comp  );
				$create_images_medium = ob_get_contents(); 
				return $create_images_medium;
			break;
			case "image/png":
				$pic = imagecreatefrompng($picture_tmp);
				$width_comp  = imagesx( $pic );
				$height_comp  = imagesy( $pic );
				$new_width_comp = floor( $width_comp * ( $size / $height_comp  ) );
				$new_height_comp  = $size;
				$tmp_img_comp  = imagecreatetruecolor( $new_width_comp , $new_height_comp  );
				$up_comp  = imagecopyresized( $tmp_img_comp , $pic, 0, 0, 0, 0, $new_width_comp , $new_height_comp , $width_comp , $height_comp  );
				$pth_comp  = site_url("images/temp/$picture_name");
				ob_start();
				$inp_comp  = imagepng( $tmp_img_comp  );
				$create_images_medium = ob_get_contents(); 
				return $create_images_medium;
			break;
			case "image/bmp":
				$pic = imagecreatefromjpeg($picture_tmp);
				$width_comp  = imagesx( $pic );
				$height_comp  = imagesy( $pic );
				$new_width_comp = floor( $width_comp * ( $size / $height_comp  ) );
				$new_height_comp  = $size;
				$tmp_img_comp  = imagecreatetruecolor( $new_width_comp , $new_height_comp  );
				$up_comp  = imagecopyresized( $tmp_img_comp , $pic, 0, 0, 0, 0, $new_width_comp , $new_height_comp , $width_comp , $height_comp  );
				$pth_comp  = site_url("images/temp/$picture_name");
				ob_start();
				$inp_comp  = imagewbmp( $tmp_img_comp  );
				$create_images_medium = ob_get_contents(); 
				return $create_images_medium;
			break;
			case "image/gif":
				$pic = imagecreatefromgif($picture_tmp);
				$width_comp  = imagesx( $pic );
				$height_comp  = imagesy( $pic );
				$new_width_comp = floor( $width_comp * ( $size / $height_comp  ) );
				$new_height_comp  = $size;
				$tmp_img_comp  = imagecreatetruecolor( $new_width_comp , $new_height_comp  );
				$up_comp  = imagecopyresized( $tmp_img_comp , $pic, 0, 0, 0, 0, $new_width_comp , $new_height_comp , $width_comp , $height_comp  );
				$pth_comp  = site_url("images/temp/$picture_name");
				ob_start();
				$inp_comp  = imagegif( $tmp_img_comp  );
				$create_images_medium = ob_get_contents(); 
				return $create_images_medium;
			break;
		}
	}
	
	protected function images_small($picture_tmp,$size,$picture_name,$picture_type){
		switch($picture_type){
			case "image/jpeg":
				$thumb = imagecreatefromjpeg($picture_tmp);
				$width = imagesx( $thumb );
				$height = imagesy( $thumb );
				$new_width = floor( $width * ( $size / $height ) );
				$new_height = $size;
				$tmp_img = imagecreatetruecolor( $new_width, $new_height );
				$up = imagecopyresized( $tmp_img, $thumb, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
				$pth = site_url("images/temp/$picture_name");
				ob_start();
				$inp = imagejpeg( $tmp_img );
				$create_images_small = ob_get_contents(); 
				return $create_images_small;
			break;
			case "image/png":
				$thumb = imagecreatefrompng($picture_tmp);
				$width = imagesx( $thumb );
				$height = imagesy( $thumb );
				$new_width = floor( $width * ( $size / $height ) );
				$new_height = $size;
				$tmp_img = imagecreatetruecolor( $new_width, $new_height );
				$up = imagecopyresized( $tmp_img, $thumb, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
				$pth = site_url("images/temp/$picture_name");
				ob_start();
				$inp = imagepng( $tmp_img );
				$create_images_small = ob_get_contents(); 
				return $create_images_small;
			break;
			case "image/bmp":
				$thumb = imagecreatefromjpeg($picture_tmp);
				$width = imagesx( $thumb );
				$height = imagesy( $thumb );
				$new_width = floor( $width * ( $size / $height ) );
				$new_height = $size;
				$tmp_img = imagecreatetruecolor( $new_width, $new_height );
				$up = imagecopyresized( $tmp_img, $thumb, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
				$pth = site_url("images/temp/$picture_name");
				ob_start();
				$inp = imagewbmp( $tmp_img );
				$create_images_small = ob_get_contents(); 
				return $create_images_small;
			break;
			case "image/gif":
				$thumb = imagecreatefromgif($picture_tmp);
				$width = imagesx( $thumb );
				$height = imagesy( $thumb );
				$new_width = floor( $width * ( $size / $height ) );
				$new_height = $size;
				$tmp_img = imagecreatetruecolor( $new_width, $new_height );
				$up = imagecopyresized( $tmp_img, $thumb, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
				$pth = site_url("images/temp/$picture_name");
				ob_start();
				$inp = imagegif( $tmp_img );
				$create_images_small = ob_get_contents(); 
				return $create_images_small;
			break;
		}
	}
	
	protected function images_tiny($picture_tmp,$size,$picture_name,$picture_type){
		switch($picture_type){
			case "image/jpeg":
				$thumb = imagecreatefromjpeg($picture_tmp);
				$width = imagesx( $thumb );
				$height = imagesy( $thumb );
				$new_width = floor( $width * ( $size / $height ) );
				$new_height = $size;
				$tmp_img = imagecreatetruecolor( $new_width, $new_height );
				$up = imagecopyresized( $tmp_img, $thumb, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
				$pth = site_url("images/temp/$picture_name");
				ob_start();
				$inp = imagejpeg( $tmp_img );
				$create_images_tiny = ob_get_contents(); 
				return $create_images_tiny;
			break;
			case "image/png":
				$thumb = imagecreatefrompng($picture_tmp);
				$width = imagesx( $thumb );
				$height = imagesy( $thumb );
				$new_width = floor( $width * ( $size / $height ) );
				$new_height = $size;
				$tmp_img = imagecreatetruecolor( $new_width, $new_height );
				$up = imagecopyresized( $tmp_img, $thumb, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
				$pth = site_url("images/temp/$picture_name");
				ob_start();
				$inp = imagepng( $tmp_img );
				$create_images_tiny = ob_get_contents(); 
				return $create_images_tiny;
			break;
			case "image/bmp":
				$thumb = imagecreatefromjpeg($picture_tmp);
				$width = imagesx( $thumb );
				$height = imagesy( $thumb );
				$new_width = floor( $width * ( $size / $height ) );
				$new_height = $size;
				$tmp_img = imagecreatetruecolor( $new_width, $new_height );
				$up = imagecopyresized( $tmp_img, $thumb, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
				$pth = site_url("images/temp/$picture_name");
				ob_start();
				$inp = imagewbmp( $tmp_img );
				$create_images_tiny = ob_get_contents(); 
				return $create_images_tiny;
			break;
			case "image/gif":
				$thumb = imagecreatefromgif($picture_tmp);
				$width = imagesx( $thumb );
				$height = imagesy( $thumb );
				$new_width = floor( $width * ( $size / $height ) );
				$new_height = $size;
				$tmp_img = imagecreatetruecolor( $new_width, $new_height );
				$up = imagecopyresized( $tmp_img, $thumb, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
				$pth = site_url("images/temp/$picture_name");
				ob_start();
				$inp = imagegif( $tmp_img );
				$create_images_tiny = ob_get_contents(); 
				return $create_images_tiny;
			break;
		}
	}
	
	protected function images_thumbs($picture_tmp,$size,$picture_name,$picture_type){
		switch($picture_type){
			case "image/jpeg":
				$thumb = imagecreatefromjpeg($picture_tmp);
				$width = imagesx( $thumb );
				$height = imagesy( $thumb );
				$new_width = floor( $width * ( $size / $height ) );
				$new_height = $size;
				$tmp_img = imagecreatetruecolor( $new_width, $new_height );
				$up = imagecopyresized( $tmp_img, $thumb, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
				$pth = site_url("images/temp/$picture_name");
				ob_start();
				$inp = imagejpeg( $tmp_img );
				$create_images_thumbs = ob_get_contents(); 
				return $create_images_thumbs;
			break;
			case "image/png":
				$thumb = imagecreatefrompng($picture_tmp);
				$width = imagesx( $thumb );
				$height = imagesy( $thumb );
				$new_width = floor( $width * ( $size / $height ) );
				$new_height = $size;
				$tmp_img = imagecreatetruecolor( $new_width, $new_height );
				$up = imagecopyresized( $tmp_img, $thumb, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
				$pth = site_url("images/temp/$picture_name");
				ob_start();
				$inp = imagepng( $tmp_img );
				$create_images_thumbs = ob_get_contents(); 
				return $create_images_thumbs;
			break;
			case "image/bmp":
				$thumb = imagecreatefromjpeg($picture_tmp);
				$width = imagesx( $thumb );
				$height = imagesy( $thumb );
				$new_width = floor( $width * ( $size / $height ) );
				$new_height = $size;
				$tmp_img = imagecreatetruecolor( $new_width, $new_height );
				$up = imagecopyresized( $tmp_img, $thumb, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
				$pth = site_url("images/temp/$picture_name");
				ob_start();
				$inp = imagewbmp( $tmp_img );
				$create_images_thumbs = ob_get_contents(); 
				return $create_images_thumbs;
			break;
			case "image/gif":
				$thumb = imagecreatefromgif($picture_tmp);
				$width = imagesx( $thumb );
				$height = imagesy( $thumb );
				$new_width = floor( $width * ( $size / $height ) );
				$new_height = $size;
				$tmp_img = imagecreatetruecolor( $new_width, $new_height );
				$up = imagecopyresized( $tmp_img, $thumb, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
				$pth = site_url("images/temp/$picture_name");
				ob_start();
				$inp = imagegif( $tmp_img );
				$create_images_thumbs = ob_get_contents(); 
				return $create_images_thumbs;
			break;
		}
	}
}